package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.*;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.FansAdapter;
import com.niji.littly_donkey_android.adapter.FollowAdapter;
import com.niji.littly_donkey_android.bean.Fans;
import com.niji.littly_donkey_android.bean.Follow;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import org.json.JSONException;

import java.util.*;

public class FollowActivity extends BaseActivity {

    private static final String TAG = "FollowActivity";

    private Button follow_return;

    private ListView follow_list;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.follow_activity);
        init();
        followReturn();
        followListClick();

    }

    private void followListClick() {
        follow_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView follow_id = findViewById(R.id.follow_id);
                String fId = follow_id.getText().toString().trim();
                int followId = Integer.parseInt(fId);
                delFollow(followId);
            }
        });
    }

    /**
     * 取消关注
     * @param followId
     */
    private void delFollow(int followId) {
        String url = UrlUtil.URL + "/user/attention/del?be_focused_uid=" + followId;
        RequestQueue requestQueue= Volley.newRequestQueue(FollowActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        String message = (String) map.get("message");
                        Toast.makeText(FollowActivity.this, message, Toast.LENGTH_SHORT).show();
                        init();
                    } else if(code == -1){
                        Toast.makeText(FollowActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(FollowActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        String message = (String) map.get("message");
                        Toast.makeText(FollowActivity.this, message, Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(FollowActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.follow_activity;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }

    private void init() {
        follow_list = findViewById(R.id.follow_list);
        String url = UrlUtil.URL + "/user/attention/list";
        ArrayList<Follow> follows = new ArrayList<>();
        RequestQueue requestQueue= Volley.newRequestQueue(FollowActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            Follow follow1 = new Follow();
                            Integer id = (Integer) mapList.get(i).get("id");
                            follow1.setId(id);
                            List<Map<String, Object>>  userInfos = (List<Map<String, Object>>) mapList.get(i).get("user_info");
                            String nickname = (String) userInfos.get(0).get("nickname");
                            String avatar = (String) userInfos.get(0).get("avatar");
                            String signature = (String) userInfos.get(0).get("signature");
                            follow1.setName(nickname);
                            follow1.setHead(UrlUtil.URL + avatar);
                            follow1.setMessage(signature);
                            Boolean follow = (Boolean) mapList.get(i).get("follow");
                            if(follow){
                                follow1.setBtn("已关注");
                            }else {
                                follow1.setBtn("互相关注");
                            }
                            follows.add(follow1);

                        }

                    } else if(code == -1){
                        Toast.makeText(FollowActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(FollowActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(getApplicationContext(), "关注列表获取失败", Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                follow_list.setAdapter(new FollowAdapter(FollowActivity.this,follows));
                RelativeLayout follow_loading = findViewById(R.id.follow_loading);
                RelativeLayout follow_main = findViewById(R.id.follow_main);
                follow_main.setVisibility(View.VISIBLE);
                follow_loading.setVisibility(View.INVISIBLE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.getMessage(), volleyError);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(FollowActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };

        requestQueue.add(stringRequest);
    }

    private void followReturn() {
        follow_return = findViewById(R.id.follow_return);
        follow_return = findViewById(R.id.follow_return);
        follow_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }
}
